package com.example.order.service.impl;

import com.example.common.util.enums.RefundStatusEnums;
import com.example.common.util.utils.DateUtils;
import com.example.common.util.utils.StringUtils;
import com.example.model.domain.entity.order.PaymentInfo;
import com.example.model.domain.entity.order.RefundInfo;
import com.example.order.mapper.RefundInfoMapper;
import com.example.order.service.RefundInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author juzipi
 * @Date 2021/7/26 8:43
 * @Info
 */
@Service
public class RefundInfoServiceImpl implements RefundInfoService {


    @Autowired
    private RefundInfoMapper refundInfoMapper;



    @Override
    public RefundInfo insertRefundInfo(PaymentInfo paymentInfo) {
        RefundInfo refundInfo = refundInfoMapper.selectRefundInfoByOrderIdAndPaymentType(paymentInfo.getOrderId(),paymentInfo.getPaymentType());
        if (StringUtils.isNull(refundInfo)){
            refundInfo = new RefundInfo();
            refundInfo.setCreateTime(DateUtils.getNowDate());
            refundInfo.setOrderId(paymentInfo.getOrderId());
            refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
            refundInfo.setRefundStatus(RefundStatusEnums.UNREFUND.getStatus());
            refundInfo.setSubject(paymentInfo.getSubject());
            refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
            refundInfoMapper.insertRefundInfo(refundInfo);
            return refundInfo;
        }
        return refundInfo;
    }


    @Override
    public Integer updateRefundInfo(RefundInfo refundInfo) {
        return refundInfoMapper.updateRefundInfo(refundInfo);
    }
}
